package org.example.one;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 消费者
 */
public class Consumer {
    public static final String QUEUE_NAME = "ONE.PRODUCER";

    public static void main(String[] args) throws IOException, TimeoutException {
        //创建一个连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("8.140.154.128");
        factory.setUsername("admin");
        factory.setPassword("admin");
        //创建一个连接
        Connection connection = factory.newConnection();
        //创建一个信道
        Channel channel = connection.createChannel();
        /**
         * 消费者消费消息
         * 1.消费哪个队列
         * 2.消费成功之后是否自动应答,false:手动应答,true:自动应答
         * 3.消费者成功消费的回调
         * 4.消费者取消消费的回调
         * 5.消息中断的回调
         */
        DeliverCallback deliverCallback = (String message, Delivery delivery) -> {
            System.out.println("消息接收到了");
            System.out.println(new String(delivery.getBody()));
        };
        CancelCallback cancelCallback = message -> {
            System.out.println("消息取消了");
            System.out.println(message);
        };
        ConsumerShutdownSignalCallback shutdownSignalCallback = (String message, ShutdownSignalException e) -> {
            System.out.println("消息异常中断了");
            System.out.println(message);
            e.printStackTrace();
        };
        channel.basicConsume(QUEUE_NAME,true,deliverCallback,cancelCallback,shutdownSignalCallback);
    }
}
